package com.smh.string;

/**
 * @description: java字符串操作
 * @author: shenmhk
 * @create: 2020-10-20 21:13
 */
public class StringTest {

    public static void main(String[] args) {
        String str = "abCdeFg";

        //1、length():统计字符串长度
        System.out.println(str.length());

        //2、indexOf:查找指定字符在字符串中的位置
        System.out.println(str.indexOf("b"));

        //3、toUpperCase：小写转大写
        System.out.println(str.toUpperCase());

        //4、toLowerCase:大写转小写
        System.out.println(str.toLowerCase());

        //5、substring：截取字符串
        System.out.println(str.substring(0, 3));//abC，取3个字符
        System.out.println(str.substring(3));//deFg 从下标3开始截取到末尾

        //6、replaceAll：替换当前字符串中指定的内容
        str.replaceAll("ab","xy");
        System.out.println(str);

        //7、trim: 去掉当前字符串中两端的空格
        String strr = " adeg efe";
        System.out.println(str.trim());//adeg efe

        //8、+：字符串拼接
        String str1 = "I love ";
        String str2 = "you!";
        System.out.println(str1 + str2);

        //9、将字符串变为整数
        String str3 = "123";
        int num = Integer.parseInt(str3);
        System.out.println(num);

        /**************************************/

        //1、charAt：获取指定下标位置的字符
        System.out.println(str.charAt(1));

        //2、toCharArray() 将字符串变成一个字符数组
        String a = "hello world";
        char[] b = str.toCharArray();
        System.out.println(b);  //输出 hello world字符数组

        //3、equals()区分大小写，equalsIgnoreCase()不区分大小写，判断二个字符串内容是否相等
        String str4 = "hello world";
        String str5 = "Hello World";
        System.out.println(str4.equals(str5));
        System.out.println(str4.equalsIgnoreCase(str5));

        //4、startsWith()判断是否以特定字符开头，endsWith()判断是否以特定字符结束
        System.out.println(str4.startsWith("ee"));//false
        System.out.println(str4.endsWith("rd"));//true

        //5、concat()连接两个字符串
        String str6 = "Hello Word";
        String str7 = "你好";
        System.out.println(str7.concat(str6));//你好Hello World

        //6、indexOf() 查找字符或字符串第一次出现的小标，lastIndexOf()查找字符或字符串最后一次出现的地方
        String str8 = "Hello Word";
        System.out.println(str8.indexOf("o"));//4
        System.out.println(str8.lastIndexOf("o"));//7

        //7、replace 替换
        String str9 = "Hello Word";
        System.out.println(str9.replace("Hello", "hello"));//hello Word

        //8、getBytes()：将字符串变成一个byte数组
        byte[] bytes = str.getBytes();
        System.out.println(new String(bytes));

        //9、compareTo() (区分大小写)，compareToIgnoreCase()(不区分大小写)
        //按字典顺序比较二个字符串的大小
        String str10 = "hello world";
        String str11 = "Hello World";
        System.out.println(str10.compareTo(str11));
        System.out.println(str10.compareToIgnoreCase(str11));

    }

}
